﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OLTS.DAL;
using OLTS.MODEL;
namespace OLTS.DATA_SERVICE
{
    public class TimeSheet_Service
    {
        private string _currentuser = "";
        private int? _month = 0;
        private OTS_TIME_SHEET _objApp = new OTS_TIME_SHEET();

        public TimeSheet_Service(string currentuser, int month)
        {
            _currentuser = currentuser;
            _month = month;
        }

        public TimeSheet_Service(OTS_TIME_SHEET objApp)
        {
            _objApp = objApp;
        }
        public TimeSheet_Service()
        {

        }
        public List<OTS_TIME_SHEET> getAll()
        {
            var ctx = new Entities();
            var result = ctx.OTS_TIME_SHEET.ToList();
            return result;
        }
        public List<OTS_TIME_SHEET> GetCurrentUserandDate(string emp_id)
        { 
            var ctx = new Entities();
            var result = ctx.OTS_TIME_SHEET.Include("OTS_WORK_DETAIL").Where(x => x.EMPLOYEE_ID == emp_id).ToList();
            return result;
        }

        public List<OTS_TIME_SHEET> GetAllinclude()
        {
            using (var ctx = new Entities())
            {
                var list = new List<OTS_TIME_SHEET>();
                var t = new OTS_TIME_SHEET();
                list = ctx.OTS_TIME_SHEET.Include("OTS_WORK_DETAIL").Where(x => x.EMPLOYEE_ID == _currentuser && x.DATE.Value.Month == _month).ToList();
                return list;
            }

        }


        public void Add()
        {
            using (var ctx = new Entities())
            {
                var alreadyObj = ctx.OTS_TIME_SHEET.Where(x => x.DATE == _objApp.DATE && x.EMPLOYEE_ID == _currentuser).FirstOrDefault();
                if (alreadyObj == null)
                {
                    ctx.OTS_TIME_SHEET.AddObject(_objApp);
                    ctx.SaveChanges();
                } 
            }
        }

        public void Add(List<OTS_TIME_SHEET> list)
        {
            var ctx = new Entities();
            foreach (var o in list)
            {
                if (o.Action == ActionEnum.Create)
                {
                    ctx.OTS_TIME_SHEET.AddObject(o);
                }
            }

            ctx.SaveChanges();
        }

        public void Edit()
        {
            using (var ctx = new Entities())
            {
                var obj = ctx.OTS_TIME_SHEET.Where(x => x.ID == _objApp.ID).FirstOrDefault();
                if (obj != null)
                {
                    obj.LEAVE_ID = _objApp.LEAVE_ID;
                    var childList = _objApp.OTS_WORK_DETAIL.ToList();
                    foreach (var o in childList)
                    {
                        var chObj = ctx.OTS_WORK_DETAIL.Where(x => x.ID == o.ID).FirstOrDefault();
                        if (chObj != null && o.Action == ActionEnum.Update)
                        {
                            chObj.AMOUNT_OF_LEAVE = o.AMOUNT_OF_LEAVE;
                            chObj.AMOUNT_OF_TIME = o.AMOUNT_OF_TIME;
                            chObj.AMOUNT_OF_TIME_OVER_TIME = o.AMOUNT_OF_TIME_OVER_TIME;
                            chObj.APPLICATION_ID = o.APPLICATION_ID;
                            chObj.APPLICATION_NAME = o.APPLICATION_NAME;
                            chObj.DETAIL = o.DETAIL;
                            chObj.START_TIME = o.START_TIME;
                            chObj.END_TIME = o.END_TIME;
                            chObj.LEAVE_ID = o.LEAVE_ID;
                            chObj.FUNCTION_NAME = o.FUNCTION_NAME;
                            chObj.JOB_CODE = o.JOB_CODE;
                            chObj.REMARK = o.REMARK;
                            chObj.REQUEST_ID = o.REQUEST_ID;
                            chObj.ROLE = o.ROLE;
                            chObj.CREATED = o.CREATED;
                            chObj.CREATE_BY = o.CREATE_BY;
                        }
                        else if (o.Action == ActionEnum.Create)
                        {
                            
                            var newobj = new OTS_WORK_DETAIL();
                            newobj.TIME_SHEET_ID = obj.ID;
                            newobj.SEQ = o.SEQ;
                            newobj.AMOUNT_OF_LEAVE = o.AMOUNT_OF_LEAVE;
                            newobj.AMOUNT_OF_TIME = o.AMOUNT_OF_TIME;
                            newobj.AMOUNT_OF_TIME_OVER_TIME = o.AMOUNT_OF_TIME_OVER_TIME;
                            newobj.DETAIL = o.DETAIL;
                            newobj.REQUEST_ID = o.REQUEST_ID;
                            newobj.START_TIME = o.START_TIME;
                            newobj.END_TIME = o.END_TIME;
                            newobj.APPLICATION_ID = o.APPLICATION_ID;
                            newobj.APPLICATION_NAME = o.APPLICATION_NAME;
                            newobj.FUNCTION_NAME = o.FUNCTION_NAME;
                            newobj.ROLE = o.ROLE;
                            newobj.LEAVE_ID = o.LEAVE_ID;
                            newobj.REMARK = o.REMARK;
                            newobj.CREATED = o.CREATED;
                            newobj.CREATE_BY = o.CREATE_BY;
                            ctx.OTS_WORK_DETAIL.AddObject(newobj);
                        }
                        else if (o.Action == ActionEnum.Delete)
                        {
                            var delObj = ctx.OTS_WORK_DETAIL.Where(x => x.ID == o.ID).FirstOrDefault();
                            if (delObj != null)
                            {
                                ctx.OTS_WORK_DETAIL.DeleteObject(delObj);
                            }
                        }

                    }
                }
                ctx.SaveChanges();
            }
        }

        public void Delete()
        {
            using (var ctx = new Entities())
            {
                var obj = ctx.OTS_TIME_SHEET.Where(x => x.ID == _objApp.ID).FirstOrDefault();
                if (obj != null)
                {
                    var childList = _objApp.OTS_WORK_DETAIL;
                    foreach (var o in childList.Where(x => x.Action == ActionEnum.Delete))
                    {
                        var delObj = ctx.OTS_WORK_DETAIL.Where(x => x.ID == o.ID).FirstOrDefault();
                        if (delObj != null)
                        {
                            ctx.OTS_WORK_DETAIL.DeleteObject(delObj);
                        }
                    }
                }

                ctx.SaveChanges();
            }
        }
    }
}
